package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.jstyle.jclife.R;
import com.jstyle.jclife.fragment.HeartHistoryDayFragment;
import com.jstyle.jclife.fragment.HeartHistoryMonthFragment;
import com.jstyle.jclife.fragment.HeartHistoryWeekFragment;
import com.jstyle.jclife.fragment.HeartHistoryYearFragment;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HeartHistoryActivity extends BaseActivity {

    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;


    @BindView(R.id.rg_history)
    RadioGroup rgHistory;
    HeartHistoryDayFragment heartHistoryDayFragment;
    HeartHistoryWeekFragment heartHistoryWeekFragment;
    HeartHistoryMonthFragment heartHistoryMonthFragment;
    HeartHistoryYearFragment heartHistoryYearFragment;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    @BindView(R.id.ll_scrollView)
    NestedScrollView llScrollView;
    private FragmentManager fragmentManager;
    private final static int FRAGMENT_DAY = 0;
    private final static int FRAGMENT_WEEK = 1;
    private final static int FRAGMENT_Month = 2;
    private final static int FRAGMENT_Year = 4;
    String queryDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart_history);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        queryDate=getIntent().getStringExtra(HealthHistoryActivity.KEY_DATA_DATE);
        fragmentManager = getSupportFragmentManager();
        startTransaction(FRAGMENT_DAY);
        btGoalBack.setText(getString(R.string.hr));
        ScreenUtils.setTitleTypeface(btGoalBack);
        rgHistory.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_heart_day:
                        startTransaction(FRAGMENT_DAY);
                        break;
                    case R.id.rb_heart_week:
                        startTransaction(FRAGMENT_WEEK);
                        break;
                    case R.id.rb_heart_month:
                        startTransaction(FRAGMENT_Month);
                        break;
                    case R.id.rb_heart_year:
                        startTransaction(FRAGMENT_Year);
                        break;
                }
            }
        });
    }

    protected void startTransaction(int fragmentId) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (fragmentId) {
            case FRAGMENT_DAY:
                if (heartHistoryDayFragment == null) {
                    heartHistoryDayFragment = HeartHistoryDayFragment.newInstance(queryDate);
                    fragmentTransaction.add(R.id.frameLayout_content, heartHistoryDayFragment);
                } else {
                    fragmentTransaction.show(heartHistoryDayFragment);
                }
                break;
            case FRAGMENT_WEEK:
                if (heartHistoryWeekFragment == null) {
                    heartHistoryWeekFragment = new HeartHistoryWeekFragment();
                    fragmentTransaction.add(R.id.frameLayout_content, heartHistoryWeekFragment);
                } else {
                    fragmentTransaction.show(heartHistoryWeekFragment);
                }
                break;
            case FRAGMENT_Month:
                if (heartHistoryMonthFragment == null) {
                    heartHistoryMonthFragment = new HeartHistoryMonthFragment();
                    fragmentTransaction.add(R.id.frameLayout_content, heartHistoryMonthFragment);
                } else {
                    fragmentTransaction.show(heartHistoryMonthFragment);
                }
                break;
            case FRAGMENT_Year:
                if (heartHistoryYearFragment == null) {
                    heartHistoryYearFragment = new HeartHistoryYearFragment();
                    fragmentTransaction.add(R.id.frameLayout_content, heartHistoryYearFragment);
                } else {
                    fragmentTransaction.show(heartHistoryYearFragment);
                }
                break;

        }

        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        hideFragment(heartHistoryWeekFragment, fragmentTransaction);
        hideFragment(heartHistoryDayFragment, fragmentTransaction);
        hideFragment(heartHistoryMonthFragment, fragmentTransaction);
        hideFragment(heartHistoryYearFragment, fragmentTransaction);

    }

    private void hideFragment(Fragment fragment, FragmentTransaction transaction) {
        if (fragment != null) {
            transaction.hide(fragment);
        }
    }

    @OnClick({R.id.iv_back, R.id.iv_share})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_share:

                ScreenUtils.shareFile(this,llScrollView);
                break;
        }
    }
}
